<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Service_Upload extends Stourweb_Controller {

    //uniapp端游记视频音频上传
    public function action_youji_upload(){
        /**
         * Array
        (
        [name] => 21e8cf50c39f6cbf55dba22a5556cfd9.mp4
        [type] => video/mp4
        [tmp_name] => D:\User\Desktop\tem\phpDF92.tmp
        [error] => 0
        [size] => 2065963
        )
         */
        $file = $_FILES['file'];
        $type = $_REQUEST['type'];
        if (empty($file)) {
            $resp = ['code' => 50001, 'data' => '', 'msg' => '请选择文件'];
            echo json_encode($resp);
            exit();
        }
        if (!in_array($type, ['video', 'audio'])) {
            $resp = ['code' => 50001, 'data' => '', 'msg' => '错误的文件类型'];
            echo json_encode($resp);
            exit();
        }
        $video_ext = ['mp4', 'mpeg', 'baiavi', 'navi', 'asf', 'mov', '3gp', 'wmv', 'divx', 'xvid', 'rm', 'rmvb', 'flv', 'f4v'];
        $audio_ext = ['mp3'];
        switch ($type) {
            case 'video':
                if ($file['size'] > 83886080) {
                    echo json_encode(['code' => 50001, 'msg' => '视频不能超过2M', 'data' => []]);
                    exit();
                }
                if(!in_array(strtolower(substr(strrchr($file['name'], '.'), 1)),$video_ext)){
                    echo json_encode(['code' => 50001, 'msg' => '视频格式不正确', 'data' => []]);
                    exit();
                }
                break;
            case 'audio':
                if ($file['size'] > 83886080) {
                    echo json_encode(['code' => 50001, 'msg' => '音频不能超过2M', 'data' => []]);
                    exit();
                }
                if(!in_array(strtolower(substr(strrchr($file['name'], '.'), 1)),$audio_ext)){
                    echo json_encode(['code' => 50001, 'msg' => '音频格式不正确', 'data' => []]);
                    exit();
                }
                break;
            default:
                break;
        }
        $pictureurl = St_Functions::local_upload_picture($file);
        if(empty($pictureurl)){
            $resp = ['code' => 50001, 'msg' => '保存失败', "data" => ''];
            echo json_encode($resp);
            exit();
        }
        $resp = ['code' => 200, 'msg' => '保存成功', "data" => $pictureurl];
        echo json_encode($resp);
        exit();
    }

}